package com.liner.work;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Homework02 {
    static Scanner scanner = new Scanner(System.in);
    static Random random = new Random();

    public static void main(String[] args) {
        //完成CommandBySwitch命令解析程序———————————练习switch
        commandBySwitch();

        //完成Guessing猜数字———————————————————练习while自造死循环
        guessing();

        //输出1到100的累加和———————————————————练习for循环
        sum();

        //完成Addition随机加法运算符———————————————练习for循环
        addition();

        //完成MuiltTable九九乘法表————————————————练习嵌套for循环
        muiltTable();

        //完成MaxOfArray求数组元素最大值并放在最后一位—————练习数组、循环
        maxOfArray();


        //完成生成N位验证码———————————————————练习数组、循环和方法
        verCode();

        //完成找到2到100之内的所有素数——————————————练习嵌套循环
        prime();

        //完成飞机票打折—————————————————————练习分支和方法
        calAirPrice();


        //完成评委打分:去掉最高分和最低分之后的平均分——————练习数组、循环和方法
        calTotalAvg();


    }

    private static void calTotalAvg() {
        System.out.print("请输入本场参与评审人数:");
        int num = scanner.nextInt();
        double[] scores = new double[num];
        for (int i = 0; i < scores.length; i++) {
            System.out.print("请输入第" + (i + 1) + "位评委的打分:");
            scores[i] = scanner.nextDouble();
        }
        Arrays.sort(scores);

        double sum = 0;
        for (int i = 1; i <scores.length -1 ; i++) {
            sum += scores[i];
        }
        double average = sum / (scores.length - 2);
        System.out.println("去掉最高分和最低分之后的平均分为:" + average);
    }

    /**
     * 输出票价
     */
    private static void calAirPrice() {
        double finalPrice = getFinalPrice();
        if (finalPrice != -1) {
            System.out.println("最终票价为:" + finalPrice);
        }
    }


    /**
     * 算出打折票价
     */
    private static double getFinalPrice() {
        double finalPrice = 0;
        System.out.print("请输入机票原价:");
        double price = scanner.nextDouble();
        System.out.print("请输入当前月份:");
        int month = scanner.nextInt();
        System.out.print("请输入订购座位(1.经济舱2.商务舱3.头等舱):");
        int type = scanner.nextInt();


        if (price < 0) {
            System.out.println("请输入正确的价格!");
            return -1;
        }
        if (month > 12 || month < 0) {
            System.out.println("请输入正确的月份!");
            return -1;
        }
        if (type > 3 || type < 0) {
            System.out.println("请输入正确座位编号!");
            return -1;
        }


        if (month >= 5 && month <= 10) {
            //5~10月 旺季  头等舱：9折 商务舱：8.5折  经济舱：8折
            switch (type) {
                case 1:
                    finalPrice = price * 0.9;
                    break;
                case 2:
                    finalPrice = price * 0.85;
                    break;
                case 3:
                    finalPrice = price * 0.8;
                    break;
            }
        } else {
            //11~4月 淡季  头等舱：7折 商务舱：6.5折  经济舱：6折
            switch (type) {
                case 1:
                    finalPrice = price * 0.7;
                    break;
                case 2:
                    finalPrice = price * 0.65;
                    break;
                case 3:
                    finalPrice = price * 0.6;
                    break;
            }
        }
        return finalPrice;
    }

    /**
     * 完成找到2到100之内的所有素数
     */
    private static void prime() {
        for (int i = 2; i <= 100; i++) {
            boolean flag = true;
            for (int j = 2; j <= i / 2; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                System.out.print(i + "\t");
            }
        }
    }

    /**
     * 完成MaxOfArray求数组元素最大值并放在最后一位
     */
    private static void maxOfArray() {
        System.out.print("请输入数组长度:");
        int len = scanner.nextInt();
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = random.nextInt(100);
        }
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        System.out.println("数组最大值:" + max);
        arr = Arrays.copyOf(arr, arr.length + 1);
        arr[arr.length - 1] = max;
        System.out.println(Arrays.toString(arr));
    }

    /**
     * 完成生成N位验证码
     */
    private static void verCode() {
        String code = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        System.out.print("请输入要生成的验证码位数:");
        int len = scanner.nextInt();
        for (int i = 0; i < len; i++) {
            int index = (int) (Math.random() * (code.length()));
            sb.append(code.charAt(index));
        }
        System.out.println(sb);
    }

    /**
     * 完成Addition随机加法运算符
     */
    private static void addition() {
        System.out.println("系统随机生成10道两位数加法,答对得10分,答错扣5分:");
        int num1, num2;
        int score = 0;
        for (int i = 1; i <= 10; i++) {
            num1 = random.nextInt(89) + 10;
            num2 = random.nextInt(89) + 10;
            System.out.println("第" + i + "题:" + num1 + " + " + num2 + " = ？");
            System.out.print("请输入你的答案:");
            int result = scanner.nextInt();
            if (result == (num1 + num2)) {
                System.out.println("恭喜你答对了! (*^▽^*)  加10分");
                score += 10;
            } else {
                System.out.println("很遗憾答错了! o(╥﹏╥)o  减5分");
                if (score != 0) {
                    score -= 5;
                }
            }
        }
        System.out.println("您的总分为:" + score);
    }

    /**
     * 完成Guessing猜数字
     */
    private static void guessing() {
        System.out.println("系统随机生成两位数数字中...");
        int rand = random.nextInt(90) + 10; //0~89 -> 10~99
        System.out.println("随机数已生成,你只有十次机会");
        int count = 10;
        while (true) {
            System.out.print("请输入你猜测的数字:");
            int guess = scanner.nextInt();
            if (guess == rand) {
                count--;
                System.out.println("恭喜你猜对了!");
                System.out.println("你一共用了" + (10 - count) + "次机会");
                break;
            } else if (guess > rand) {
                System.out.println("很遗憾,猜大了");
                count--;
            } else {
                System.out.println("很遗憾,猜小了");
                count--;
            }

            if (count == 0) {
                System.out.println("对不起,机会用完了,请下次再来");
                break;
            }

        }
    }

    /**
     * 输出1到100的累加和
     */
    private static void sum() {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println("1到100的累加和为:" + sum);
    }

    /**
     * 完成MuiltTable九九乘法表
     */
    private static void muiltTable() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " * " + i + " = " + i * j + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 完成CommandBySwitch命令解析程序
     */
    private static void commandBySwitch() {
        boolean flag = true;
        int choice = 0;

        System.out.println("欢迎光临我的客栈");
        while (flag) {
            System.out.print("请输入你的选择(1.办理住房 2.办理退房 3.办理会员 4.退出系统):");
            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();
            }
            switch (choice) {
                case 1:
                    System.out.println("办理住房:");
                    System.out.println("......");
                    System.out.println("办理结束");
                    break;
                case 2:
                    System.out.println("办理退房:");
                    System.out.println("......");
                    System.out.println("办理结束");
                    break;
                case 3:
                    System.out.println("办理会员:");
                    System.out.println("......");
                    System.out.println("办理结束");
                    break;
                case 4:
                    System.out.println("退出系统");
                    flag = false;
                    break;
                default:
                    System.out.println("抱歉我做不到");
                    flag = false;
            }
        }
    }
}
